package com.roads.project.controller;

import java.util.List;

import com.roads.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.project.domain.ProCompany;
import com.roads.project.service.IProCompanyService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 单位（公司）Controller
 * 
 * @author nhp
 * @date 2021-11-16
 */
@RestController
@RequestMapping("/project/company")
public class ProCompanyController extends BaseController {

    @Autowired
    private IProCompanyService proCompanyService;

    /**
     * 查询单位（公司）列表
     */
    @PreAuthorize("@ss.hasPermi('project:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProCompany proCompany) {
        startPage();
        List<ProCompany> list = proCompanyService.selectProCompanyList(proCompany);
        return getDataTable(list);
    }

    /**
     * 导出单位（公司）列表
     */
    @PreAuthorize("@ss.hasPermi('project:company:export')")
    @Log(title = "单位（公司）", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProCompany proCompany) {
        List<ProCompany> list = proCompanyService.selectProCompanyList(proCompany);
        ExcelUtils<ProCompany> util = new ExcelUtils<ProCompany>(ProCompany.class);
        return util.exportExcel(list, "单位（公司）数据");
    }

    /**
     * 获取单位（公司）详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Integer companyId) {
        return AjaxResult.success(proCompanyService.selectProCompanyByCompanyId(companyId));
    }

    /**
     * 新增单位（公司）
     */
    @PreAuthorize("@ss.hasPermi('project:company:add')")
    @Log(title = "单位（公司）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProCompany proCompany) {
        if (UserConstants.NOT_UNIQUE.equals(proCompanyService.checkCompanyNameUnique(proCompany))) {
            return AjaxResult.error("新增单位'" + proCompany.getCompanyName() + "'失败，单位名称已存在");
        }
        proCompany.setCreateBy(getUsername());
        return toAjax(proCompanyService.insertProCompany(proCompany));
    }

    /**
     * 修改单位（公司）
     */
    @PreAuthorize("@ss.hasPermi('project:company:edit')")
    @Log(title = "单位（公司）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProCompany proCompany) {
        if (UserConstants.NOT_UNIQUE.equals(proCompanyService.checkCompanyNameUnique(proCompany))) {
            return AjaxResult.error("修改单位'" + proCompany.getCompanyName() + "'失败，单位名称已存在");
        }
        proCompany.setUpdateBy(getUsername());
        return toAjax(proCompanyService.updateProCompany(proCompany));
    }

    /**
     * 删除单位（公司）
     */
    @PreAuthorize("@ss.hasPermi('project:company:remove')")
    @Log(title = "单位（公司）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Integer[] companyIds) {
        return toAjax(proCompanyService.deleteProCompanyByCompanyIds(companyIds));
    }
}
